package pl.lodz.p.browarex.model;

import java.util.Date;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import javax.persistence.Table;

import org.springframework.stereotype.Repository;

import pl.lodz.p.browarex.base.interfaces.IBaseModel;
import pl.lodz.p.browarex.model.enums.OrderState;

@Entity
@Table(name = "PRODUCT_ORDER")
@Repository
public class Order implements IBaseModel<Long> {

	private Long id;
	private String orderID;
	private Date orderDate;
	private Customer customer;
	private String customerID;
	private OrderState orderState;
	private List<OrderProductInfo> productList;

	@Id
	@GeneratedValue(strategy = GenerationType.AUTO)
	@Column(name = "ORDER_ID")
	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	@Column(name = "ORDER_ID_STRING", unique = true, nullable = false)
	public String getOrderID() {
		return orderID;
	}

	public void setOrderID(String orderID) {
		this.orderID = orderID;
	}

	@Column(name = "ORDERDATE", unique = false, nullable = false)
	public Date getOrderDate() {
		return orderDate;
	}

	public void setOrderDate(Date orderDate) {
		this.orderDate = orderDate;
	}

	@OneToOne
	@JoinColumn(name = "CUSTOMER_ID", unique = false, nullable = false)
	public Customer getCustomer() {
		return customer;
	}

	public void setCustomer(Customer customer) {
		this.customer = customer;
		this.customerID = this.customer.getCustomerID();
	}

	@Column(name = "CUSTOMER_STRING_ID", unique = false, nullable = false)
	public String getCustomerID() {
		return customerID;
	}

	public void setCustomerID(String customerID) {
		if (this.customer != null) {
			this.customerID = this.customer.getCustomerID();
		}
	}

	@OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
	public List<OrderProductInfo> getProductList() {
		return productList;
	}

	public void setProductList(List<OrderProductInfo> productList) {
		this.productList = productList;
	}

	@Enumerated(EnumType.STRING)
	@Column(name = "ORDER_STATE", unique = false, nullable = false)
	public OrderState getOrderState() {
		return orderState;
	}

	public void setOrderState(OrderState orderState) {
		this.orderState = orderState;
	}

}
